/** 领有惠测试库baseUrl */
//const URL = 'https://t-appportal.meitianhui.com';


/** 领有惠正式库baseUrl */
const URL = 'https://appportal.meitianhui.com';



//const localhostURL = 'http://localhost:3001';

const MD5Utils = require('MD5.js');
const util = require('util.js');


//加入权限判断标记mark.需要判断权限mark为true,不需要判断mark为flase
const wxRequest = (params, url, mark) => {
  if (mark) {
    var value = wx.getStorageSync('memberInfo')
    console.log("-----------本地缓存的数据 memberInfo----------------")
    console.log(value)

    //从缓存里根据key查询登陆信息，如果存在则继续，不存在则跳转登陆页面
    if (value == '') {//判断微信缓存里是否有登陆信息
      //拦截需要登陆的请求跳转登陆页面，不需要登陆的请求正常访问(/openapi/goods)
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
  }

  wx.request({
    url,
    method: params.method || 'GET',
    data: params.data || {},
    header: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    //收到开发者服务成功返回的回调函数
    success(res) {
      if (params.success) {
        params.success(res);
        console.log(res)
      }
    },
    //接口调用失败的回调函数
    fail(res) {
      if (params.fail) {
        params.fail(res);
        console.log(res)
      }
    },
    //接口调用结束的回调函数（调用成功、失败都会执行）
    complete(res) {
      if (params.complete) {
        params.complete(res);
        console.log(res)
      }
    },
  });
};



//通过ops-smallprogram程序去请求数据
const reqData = (params) => {
  var urlType = params.data.url_type;
  var security_code = params.data.security_code
  //var service = params.data.service

  var url;
  const data = {
    service: params.data.service,
    params: params.data.params,
    app_token: params.data.app_token,
    page: params.data.page,
  }

  data.sign = getSign(data, security_code);
  params.data = data;

  var needLogin = false;//需不需要登陆

  if (urlType == 'finance') {
    //url = "http://localhost:8080" + "/ops-finance/finance";
    url = URL + "/openapi/finance";
    
    needLogin = true;
  } else if (urlType == 'member') {
    url = URL + "/openapi/member";
    needLogin = true;
  } else if (urlType == 'goods') {
    url = URL + "/openapi/goods";
  } else if (urlType == '/openapi/trade/reg') {
    url = URL + "/openapi/trade/reg";
  } else if (urlType == 'opsorder') {
    url = URL + "/openapi/opsorder";
    needLogin = true;
  }

  console.log("")
  console.log("url data >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  console.log(url)
  console.log(data)
  console.log("")


  wxRequest(params, url, needLogin);
};





//此方法wxRequest时不需要判断是否要登陆（特殊方法）
const requestData = (params) => {


  var urlType = params.data.url_type;
  var security_code = params.data.security_code;
  //var service = params.data.service;
  var url;

  const data = {
    service: params.data.service,
    params: params.data.params,
    app_token: params.data.app_token,
    //page: params.data.page,
  }


  if (params.data.page != null) {
    data.page = params.data.page
  }


  data.sign = getSign(data, security_code);
  params.data = data;


  if (urlType == 'finance') {
    url = URL + "/openapi/finance";
  } else if (urlType == 'member') {
    url = URL + "/openapi/member";
  } else if (urlType == 'goods') {
    url = URL + "/openapi/goods";
  } else if (urlType == '/openapi/trade/reg') {
    url = URL + "/openapi/trade/reg";
  } else if (urlType == 'opsorder') {
    url = URL + "/openapi/opsorder";
  }

  console.log("")
  console.log("url data >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  console.log(url)
  console.log(data)
  console.log("")

  wxRequest(params, url, false);

};







/** 领有惠APP登陆 */
const getTokenAndCode = (params) => {
  wxRequest(params, `${URL}/openapi/app/login`);
};


//根据经纬度获取百度地图上的城市地理信息
const getAddressByLocation = (params) => {
  wxRequest(params, "https://api.map.baidu.com/geocoder/v2/", false);
};




function getSign(data, code) {

  var newkey = Object.keys(data).sort();
  var sign = "";

  for (var i = 0; i < newkey.length; i++) {//遍历newObj数组
    if (i != newkey.length - 1) {
      sign += newkey[i];
      sign += "=";
      sign += data[newkey[i]];
      sign += "&";
    } else {
      sign += newkey[i];
      sign += "=";
      sign += data[newkey[i]];
    }

  }
  sign += code;
  return MD5Utils.hexMD5(sign);
}



const reqDataTemp = (params) => {
  var urlType = params.data.url_type;
  var security_code = params.data.security_code

  var url;
  const data = {
    service: params.data.service,
    params: params.data.params,
    app_token: params.data.app_token,
    page: params.data.page,
  }

  console.log("data>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  console.log(data)

  data.sign = getSign(data, security_code);

  console.log("sign>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
  console.log(getSign(data, security_code))



  params.data = data;


  var needLogin = false;//需不需要登陆

  if (urlType == 'finance') {
    url = URL + "/openapi/finance";
    needLogin = true;
  } else if (urlType == 'member') {
    url = URL + "/openapi/member";
    needLogin = true;
  } else if (urlType == 'goods') {
    url = URL + "/openapi/goods";
  } else if (urlType == '/openapi/trade/reg') {
    url = URL + "/openapi/trade/reg";
  } else if (urlType == 'opsorder') {
    url = URL + "/openapi/opsorder";
    needLogin = true;
  }

  wxRequest(params, url, needLogin);
};







//付款码条码和二维码专用
const requestCode = (params) => {
  var urlType = params.data.url_type;
  var security_code = params.data.security_code


  var url;
  const data = {
    service: params.data.service,
    params: params.data.params,
    //user_token: user_token,//和requestAction方法仅仅这个参数不同

    app_token: params.data.app_token,

    page: params.data.page,
  }

  data.sign = getSign(data, security_code);
  params.data = data;

  if (urlType == 'finance') {
    url = URL + "/openapi/finance";
  }

  wxRequest(params, url, true);
};








//零钱支付接口专用
/* const requestPay = (params) => {
  var url;
  var security_code = params.data.security_code


  const data = {
    service: params.data.service,
    params: params.data.params,
    user_token: params.data.app_token,///// 注意这里是 user_token
    //user_token:'d13afc37a60adf8b896720f71c8df0d6e3be67848150ea9eee779a434fb733d29bd06adce317e51caa4d168d11c9a354'
  }

  data.sign = getSign(data, security_code);
  //data.sign = util.getSignWithnotSecutiryCode(data);
  params.data = data;
  //params.method = 'POST'

  url = URL + "/openapi/finance";

  console.log("params>>>>>>>>>>>>>>>>>>")
  console.log(params)

  wxRequest(params, url, true);
}; */







//发送验证码
const sendsmscode = (params) => {
  var url;
  var security_code = params.data.security_code
  const data = {
    mobile: params.data.mobile,
    app_token: params.data.app_token,
  }

  data.sign = getSign(data, security_code);
  params.data = data;
  url = URL + "/openapi/notify/send_sms_code";

  wxRequest(params, url);
};




//会员登陆
const memberLogin = (params) => {
  var url;
  var security_code = params.data.security_code
  const data = {
    member_type_key: params.data.member_type_key,
    data_source: params.data.data_source,
    mobile: params.data.mobile,
    device_model: params.data.device_model,
    sms_code: params.data.sms_code,
    request_info: params.data.request_info,
    app_token: params.data.app_token,

  }

  data.sign = getSign(data, security_code);
  params.data = data;

  url = URL + "/openapi/consumer/login";

  wxRequest(params, url);
};


//会员验证
const userValidate = (params) => {
  var url;
  var security_code = params.data.security_code
  const data = {
    user_token: params.data.user_token,
/*     request_info: params.data.request_info, */
    //app_token: params.data.app_token,
  }

  data.sign = getSign(data, security_code);
  params.data = data;

  url = URL + "/openapi/user/verify_user_token";
  wxRequest(params, url);
};






module.exports = {
  getTokenAndCode,
  getAddressByLocation,
  reqDataTemp,
  reqData,
  requestData,
  requestCode,
  sendsmscode,
  memberLogin,
  userValidate
};
